package com.yj.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import com.yj.dao.AuthMapper;
import com.yj.entity.Users;

/**
 * 拦截器二
 * @author 大大大
 *
 */
@Component
public class TwoInterceptor implements HandlerInterceptor{
	
	@Autowired
	AuthMapper auth;

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		HttpSession session = request.getSession();
		Users user = (Users)session.getAttribute("usersession");
		if(user!=null) {
			Integer uid = user.getUid();
			String admin = auth.isAdmin(uid);
			if(admin.equals("admin")) {
				return true;
			}
			else {
				response.sendRedirect("http://localhost:9090/District_10/login.html");
				return false;
			}
		}else {
			response.sendRedirect("http://localhost:9090/District_10/login.html");
			return false;
		}
	}
	
}
